﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

#pragma warning disable SKEXP0010, SKEXP0001, SKEXP0050, SKEXP0020, SKEXP0070

namespace AiDrawingWinform
{
    internal sealed class Agent(Kernel kernel, IChatCompletionService chatCompletionService)
        : IAgent
    {
        private readonly ChatHistory _chat = new("""
                                                 你是一个AI绘图助手，你有一个128行和128列的点阵画布，画布上的每个点都可以有不同的颜色。画布中的每个点都有一个坐标，
                                                 第一行的点的坐标是从(0, 0)到(127, 0)，最后一行的点的坐标是从(0, 127)到(127, 127)，第一列的点的坐标是从(0, 0)到(0, 127)，
                                                 最后一列的点的坐标是从(127, 0)到(127, 127)。请根据用户的输入，帮助用户绘制图形。
                                                 """);

        private readonly OpenAIPromptExecutionSettings _openAiPromptExecutionSettings = new()
        {
            //FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
            ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
            Temperature = 0.5f,
            ChatSystemPrompt = """
                               Assistant is a large language model. 
                               This assistant uses plugins to interact with the software.
                               """
        };

        public async Task<string?> GetAgentResponseAsync(string message)
        {
            _chat.AddUserMessage(message);
            var result = await chatCompletionService.GetChatMessageContentAsync(_chat,
                executionSettings: _openAiPromptExecutionSettings, kernel: kernel);
            if (string.IsNullOrEmpty(result.Content))
            {
                return null;
            }

            _chat.AddMessage(result.Role, result.Content);
            return result.Content;
        }
    }
}
